#include "log.h"


SettingLogger::SettingLogger() { }
SettingLogger::~SettingLogger() { }

PlayingLogger::PlayingLogger()  { }
PlayingLogger::~PlayingLogger() { }


NettingLogger::NettingLogger()  { }
NettingLogger::~NettingLogger() { }


bool Logger::log(string& msg) {
  file.seekp(ios::end);
  file << GetSystemTime() << LogLevelName[loglevel] << msg << endl;
 /*
  flush_num++;
  if(flush_num >= MAX_FLUSH_NUM) {
    flush_num = 0;
    if(file.is_open()) file.flush();

  }*/
    return true;
}


Logger::Logger(ENUM_LOG_LEVEL level):flush_num(0) {

}

Logger::Logger() { }
Logger::~Logger() { }




void SettingLogger::LogConfig() {
  time_t v_time;
  time(&v_time);
  tm* nowtime = localtime(&v_time);
	//oldtime=nowtime;
  oldtime.tm_year = nowtime->tm_year;
	oldtime.tm_mon  = nowtime->tm_mon;
	oldtime.tm_mday = nowtime->tm_mday;
  ostringstream oss;
  string base_name = "SETTING_LOGFILE";
  oss << base_name << "_" << nowtime->tm_year + 1900 << "_" <<
      setw(2) << setfill('0')  << nowtime->tm_mon + 1 << "_" <<
      setw(2) << setfill('0')  << nowtime->tm_mday;

  filename = base_name;
  filepath = "./log/" + oss.str();
  cout << filename << "\n";
  cout << filepath << "\n";
}


void PlayingLogger::LogConfig() {
  time_t v_time;
  time(&v_time);
  tm* nowtime = localtime(&v_time);
	//oldtime=nowtime;
  oldtime.tm_year = nowtime->tm_year;
	oldtime.tm_mon  = nowtime->tm_mon;
	oldtime.tm_mday = nowtime->tm_mday;
  ostringstream oss;
  string base_name = "PLAYING_LOGFILE";
  oss << base_name << "_" << nowtime->tm_year + 1900 << "_" <<
      setw(2) << setfill('0')  << nowtime->tm_mon + 1 << "_" <<
      setw(2) << setfill('0')  << nowtime->tm_mday;

  filename = base_name;
  filepath = "./log/" + oss.str();
  cout << filename << "\n";
  cout << filepath << "\n";
}



void NettingLogger::LogConfig() {
  time_t v_time;
  time(&v_time);
  tm* nowtime = localtime(&v_time);
	//oldtime=nowtime;
  oldtime.tm_year = nowtime->tm_year;
	oldtime.tm_mon  = nowtime->tm_mon;
	oldtime.tm_mday = nowtime->tm_mday;
  ostringstream oss;
  string base_name = "NETTING_LOGFILE";
  oss << base_name << "_" << nowtime->tm_year + 1900 << "_" <<
      setw(2) << setfill('0')  << nowtime->tm_mon + 1 << "_" <<
      setw(2) << setfill('0')  << nowtime->tm_mday;

  filename = base_name;
  filepath = "./log/" + oss.str();
  cout << filename << "\n";
  cout << filepath << "\n";
}


bool Logger::SetLevel(ENUM_LOG_LEVEL newlevel) {

  if(newlevel >= LOG_LEVEL_END) {
    printf("the newlevel configed is invalid\n");
    return false;
  }
  else if(loglevel != newlevel) {
    loglevel = newlevel;
    file.seekp(ios::end);
    file << "the log level is set " << LogLevelName[loglevel] << endl;
  }
  else if(loglevel == newlevel) {
    //file << "the log level is not change"  << LogLevelName[loglevel] << "\n";
  }
  return true;
}

bool Logger::IfRotateLog() {

  time_t v_time;
  time(&v_time);
  tm* newtime = localtime(&v_time);

  if(newtime->tm_year == oldtime.tm_year &&
     newtime->tm_mon  == oldtime.tm_mon  &&
     newtime->tm_mday == oldtime.tm_mday ) {
    //cout << "no need rotate log " << "\n";
    return false;
  }
  else{
	  oldtime.tm_year = newtime->tm_year;
		oldtime.tm_mon  = newtime->tm_mon;
		oldtime.tm_mday = newtime->tm_mday;
    cout << "need rotate log" << "\n";
    return true;
	}
}

bool Logger::RotateLog() {


  CloseFile();
	string rm="rm "+filepath;
	system(rm.c_str());
  time_t v_time;
  time(&v_time);
  tm* newtime = localtime(&v_time);
  ostringstream oss;
  oss << "./log/"<< filename << "_" << newtime->tm_year + 1900 << "_" <<
      setw(2) << setfill('0')  << newtime->tm_mon + 1 << "_" <<
      setw(2) << setfill('0')  << newtime->tm_mday;
  filepath = oss.str(); 
  OpenFile();
  return true;
}

string Logger::GetSystemTime() {
  time_t NowTime;
  time(&NowTime);
  tm* LocalTime = localtime(&NowTime);
  char szTime[30] = {'\0'};
  strftime(szTime,30,"[%Y-%m-%d %H-%M-%S] ", LocalTime);
  string strTime = szTime;
  return strTime;
}

bool Logger::OpenFile() {

  //cout << "cnt = " << Logger::cnt << endl; 
  cout << "file.good() = " << file.good() << endl;
  cout << "file.is_open() = " << file.is_open() <<endl;
  if(file.is_open()) {
    return true;
  }
  
  ios_base::openmode mode = fstream::in |fstream::out | fstream::app;
  file.open(filepath.c_str(), mode);

  if(!file.good()){ 
    printf("fail to open the file <%s>!", filename.c_str()); 
    return false;
  }
  return true;
}

bool Logger::CloseFile() {

  if(file.is_open()) {
    file.close();
    return true;
  } 
  else {
    return false;
  }
}
